﻿using ChurchDiary.Common;
using ChurchDiary.DAL.Contract;
using ChurchDiary.DAL.DAL;
using ChurchDiary.TableEntity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChurchDiary.DAL.Methods
{
    public class DAL_User : DataAccess, IUser
    {
        #region User
        public EntityUser GetUserInfo(string Username, string Password)
        {
            EntityUser User = new EntityUser();
            try
            {
                using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[GET_Userdetails]"))
                {
                    using (DbConnection con = SQLServer.CreateConnection())
                    {
                        SQLServer.AddInParameter(dbCommand, "@mode", DbType.String, "LOGIN");
                        SQLServer.AddInParameter(dbCommand, "@username", DbType.String, Username);
                        SQLServer.AddInParameter(dbCommand, "@Password", DbType.String, Password);
                        using (IDataReader OReader = SQLServer.ExecuteReader(dbCommand))
                        {
                            if (OReader == null)
                            {
                                return User;
                            }
                            while (OReader.Read())
                            {
                                User.Userid = (Guid)OReader["Userid"];
                                User.UserName = OReader["UserName"].ToString();
                                User.Password = OReader["Password"].ToString();
                                User.Emailid = OReader["Emailid"].ToString();
                                if (!string.IsNullOrEmpty(OReader["LastLogin"].ToString()))
                                {
                                    User.LastLogin = (DateTime)OReader["LastLogin"];
                                }
                                User.LoginCount = (Int32)OReader["LoginCount"];
                                User.Name = OReader["Name"].ToString();
                                User.IsSuperAdmin = (bool)OReader["IsSuperAdmin"];
                                User.ChurchId = (Guid)OReader["ChurchId"];
                                User.Status = (Int32)OReader["Status"];
                                User.Image = OReader["Image"].ToString();
                            }
                            OReader.NextResult();
                            User.UserLoginTypes = new List<EntityUserLoginType>();
                            while (OReader.Read())
                            {
                                EntityUserLoginType UserLogintype = new EntityUserLoginType();
                                UserLogintype.UserId = (Guid)OReader["UserId"];
                                UserLogintype.UserLoginTypeId = (Guid)OReader["UserLoginTypeId"];
                                UserLogintype.UserTypeId = (Guid)OReader["UserTypeId"];
                                UserLogintype.UserTypeName = OReader["UserTypeName"].ToString();
                                User.UserLoginTypes.Add(UserLogintype);
                            }
                            OReader.NextResult();
                            User.UserSettings = new EntityUserSettings();
                            while (OReader.Read())
                            {
                                User.UserSettings.Color = OReader["Color"].ToString();
                                User.UserSettings.CountryId = (Guid)OReader["CountryId"];
                                User.UserSettings.CurencyValue = OReader["CurencyValue"].ToString();
                                User.UserSettings.DateFormateId = (Guid)OReader["DateFormateId"];
                                User.UserSettings.DetaultAddress1 = OReader["DetaultAddress1"].ToString();
                                User.UserSettings.DetaultAddress2 = OReader["DetaultAddress2"].ToString();
                                User.UserSettings.DetaultPinCode = OReader["DetaultPinCode"].ToString();
                                User.UserSettings.EmailNotification = (bool)OReader["EmailNotification"];
                                User.UserSettings.DetaultPinCode = OReader["DetaultPinCode"].ToString();
                                User.UserSettings.fixedHead = (bool)OReader["fixedHead"];
                                User.UserSettings.LanguageId = (Guid)OReader["LanguageId"];
                                User.UserSettings.NotificationSound = (bool)OReader["NotificationSound"];
                                User.UserSettings.NotificationPosition = OReader["NotificationPosition"].ToString();
                                User.UserSettings.SettingStatus = OReader["SettingStatus"].ToString();
                                User.UserSettings.StateId = (Guid)OReader["StateId"];
                                User.UserSettings.TimeFormat = OReader["TimeFormat"].ToString();
                                User.UserSettings.UserPicture = CommonFunction.GetOrginalImagePath(OReader["UserPicture"].ToString(), IMAGEPATH.USERIMAGE);
                                User.UserSettings.CoverPicture = CommonFunction.GetOrginalImagePath(OReader["CoverPicture"].ToString(), IMAGEPATH.USERCOVERIMAGE);
                                User.UserSettings.SettingPercentage = OReader["SettingPercentage"].ToString();
                            }
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
            }
            return User;
        }
        public EntityUser GetUserInfo(Guid UserId)
        {
            EntityUser User = new EntityUser();
            try
            {
                using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[GET_Userdetails]"))
                {
                    using (DbConnection con = SQLServer.CreateConnection())
                    {
                        SQLServer.AddInParameter(dbCommand, "@mode", DbType.String, "USERDETAILS");
                        SQLServer.AddInParameter(dbCommand, "@UsersId", DbType.Guid, UserId);
                        using (IDataReader OReader = SQLServer.ExecuteReader(dbCommand))
                        {
                            if (OReader == null)
                            {
                                return User;
                            }
                            while (OReader.Read())
                            {
                                User.Userid = (Guid)OReader["Userid"];
                                User.UserName = OReader["UserName"].ToString();
                                User.Password = OReader["Password"].ToString();
                                User.Emailid = OReader["Emailid"].ToString();
                                if (!string.IsNullOrEmpty(OReader["LastLogin"].ToString()))
                                {
                                    User.LastLogin = (DateTime)OReader["LastLogin"];
                                }
                                User.LoginCount = (Int32)OReader["LoginCount"];
                                User.Name = OReader["Name"].ToString();
                                User.IsSuperAdmin = (bool)OReader["IsSuperAdmin"];
                                User.ChurchId = (Guid)OReader["ChurchId"];
                                User.Status = (Int32)OReader["Status"];
                                User.Image = OReader["Image"].ToString();
                            }
                            OReader.NextResult();
                            User.UserLoginTypes = new List<EntityUserLoginType>();
                            while (OReader.Read())
                            {
                                EntityUserLoginType UserLogintype = new EntityUserLoginType();
                                UserLogintype.UserId = (Guid)OReader["UserId"];
                                UserLogintype.UserLoginTypeId = (Guid)OReader["UserLoginTypeId"];
                                UserLogintype.UserTypeId = (Guid)OReader["UserTypeId"];
                                UserLogintype.UserTypeName = OReader["UserTypeName"].ToString();
                                User.UserLoginTypes.Add(UserLogintype);
                            }
                            
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
            }
            return User;
        }
        #endregion User
        public EntityUserSettings GetUserSettings(Guid UserId)
        {
            EntityUserSettings UserSettings = new EntityUserSettings();
            try
            {
                using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[GET_UserSettings]"))
                {
                    using (DbConnection con = SQLServer.CreateConnection())
                    {
                        SQLServer.AddInParameter(dbCommand, "@mode", DbType.String, eMode.ONE);
                        SQLServer.AddInParameter(dbCommand, "@UserId", DbType.Guid, UserId);
                        using (IDataReader OReader = SQLServer.ExecuteReader(dbCommand))
                        {
                            if (OReader == null)
                            {
                                return UserSettings;
                            }
                            while (OReader.Read())
                            {
                                UserSettings.Color = OReader["Color"].ToString();
                                UserSettings.CountryId = (Guid)OReader["CountryId"];
                                UserSettings.CurencyValue = OReader["CurencyValue"].ToString();
                                UserSettings.DateFormateId = (Guid)OReader["DateFormateId"];
                                UserSettings.DetaultAddress1 = OReader["DetaultAddress1"].ToString();
                                UserSettings.DetaultAddress2 = OReader["DetaultAddress2"].ToString();
                                UserSettings.DetaultPinCode = OReader["DetaultPinCode"].ToString();
                                UserSettings.EmailNotification = (bool)OReader["EmailNotification"];
                                UserSettings.DetaultPinCode = OReader["DetaultPinCode"].ToString();
                                UserSettings.fixedHead = (bool)OReader["fixedHead"];
                                UserSettings.LanguageId = (Guid)OReader["LanguageId"];
                                UserSettings.NotificationSound = (bool)OReader["NotificationSound"];
                                UserSettings.NotificationPosition = OReader["NotificationPosition"].ToString();
                                UserSettings.SettingStatus = OReader["SettingStatus"].ToString();
                                UserSettings.StateId = (Guid)OReader["StateId"];
                                UserSettings.TimeFormat = OReader["TimeFormat"].ToString();
                                UserSettings.UserPicture = CommonFunction.GetOrginalImagePath(OReader["UserPicture"].ToString(), IMAGEPATH.USERIMAGE);
                                UserSettings.CoverPicture = CommonFunction.GetOrginalImagePath(OReader["CoverPicture"].ToString(), IMAGEPATH.USERCOVERIMAGE);
                                UserSettings.SettingPercentage = OReader["SettingPercentage"].ToString();

                                UserSettings.Language = new EntityLanguage();
                                UserSettings.Language.LanguageName = OReader["LanguageName"].ToString();
                                UserSettings.DateFormat = new EntityDateFormat();
                                UserSettings.DateFormat.DateFormatName = OReader["DateFormatName"].ToString();
                                UserSettings.Country = new EntityCountry();
                                UserSettings.Country.CountryName = OReader["CountryName"].ToString();
                                UserSettings.States = new EntityStates();
                                UserSettings.States.StateName = OReader["StateName"].ToString();
                            }
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
            }
            return UserSettings;
        }
        public string UpdateUserSettings(EntityUserSettings Settings)
        {
            string IResult = "SME";

            using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[U_UserSettings]"))
            {
                SQLServer.AddInParameter(dbCommand, "@UserId", DbType.Guid, Current.Userid);
                SQLServer.AddInParameter(dbCommand, "@CoverPicture", DbType.String, CommonFunction.UploadImage(IMAGEPATH.USERCOVERIMAGE, Settings.CoverPicture));
                SQLServer.AddInParameter(dbCommand, "@UserPicture", DbType.String, CommonFunction.UploadImage(IMAGEPATH.USERIMAGE, Settings.UserPicture));
                SQLServer.AddInParameter(dbCommand, "@DateFormateId", DbType.Guid, Settings.DateFormateId);
                SQLServer.AddInParameter(dbCommand, "@Color", DbType.String, Settings.Color);
                SQLServer.AddInParameter(dbCommand, "@LanguageId", DbType.Guid, Settings.LanguageId);
                SQLServer.AddInParameter(dbCommand, "@TimeFormat", DbType.String, Settings.TimeFormat);
                SQLServer.AddInParameter(dbCommand, "@NotificationSound", DbType.Boolean, Settings.NotificationSound);
                SQLServer.AddInParameter(dbCommand, "@EmailNotification", DbType.Boolean, Settings.EmailNotification);
                SQLServer.AddInParameter(dbCommand, "@StateId", DbType.Guid, Settings.StateId);
                SQLServer.AddInParameter(dbCommand, "@DetaultAddress1", DbType.String, Settings.DetaultAddress1);
                SQLServer.AddInParameter(dbCommand, "@DetaultAddress2", DbType.String, Settings.DetaultAddress2);
                SQLServer.AddInParameter(dbCommand, "@DetaultPinCode", DbType.String, Settings.DetaultPinCode);
                SQLServer.AddInParameter(dbCommand, "@Updatedby", DbType.Guid, Current.Userid);
                SQLServer.AddInParameter(dbCommand, "@UpdatedOn", DbType.DateTime, DateTime.Now);
                SQLServer.AddInParameter(dbCommand, "@fixedHead", DbType.Boolean, Settings.fixedHead);
                SQLServer.AddInParameter(dbCommand, "@CountryId", DbType.Guid, Settings.CountryId);
                SQLServer.AddInParameter(dbCommand, "@NotificationPosition", DbType.String, Settings.NotificationPosition);
                using (DbConnection con = SQLServer.CreateConnection())
                {
                    IResult = (string)SQLServer.ExecuteScalar(dbCommand);
                }
            }
            
            return IResult;
        }

        #region UserType
        public EntityUserType GetUserType(Guid userTypeId)
        {
            EntityUserType userType = new EntityUserType();
            try
            {
                using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[GET_UserType]"))
                {
                    using (DbConnection con = SQLServer.CreateConnection())
                    {
                        SQLServer.AddInParameter(dbCommand, "@Mode", DbType.String, GETMODE.ONE);
                        SQLServer.AddInParameter(dbCommand, "@UserTypeId", DbType.Guid, userTypeId);
                        using (IDataReader OReader = SQLServer.ExecuteReader(dbCommand))
                        {
                            if (OReader == null)
                            {
                                return userType;
                            }
                            while (OReader.Read())
                            {
                                userType.UserTypeId = (Guid)OReader["UserTypeId"];
                                userType.SubMenu = OReader["SubMenu"].ToString();
                                userType.MenuCategory = OReader["MenuCategory"].ToString();
                                userType.MainMenu = OReader["MainMenu"].ToString();
                                userType.landingPage = (Int32)OReader["landingPage"];
                                userType.ControllerName = OReader["ControllerName"].ToString();
                                userType.ActionName = OReader["ActionName"].ToString();
                                userType.AreaName = OReader["Area"].ToString();
                            }
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
            }
            return userType;
        }
        #endregion UserType

    }
}
